package com.xncoding.netty.coder.tcp;

import java.nio.ByteBuffer;
import java.util.List;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
public class ByteDecoder extends ByteToMessageDecoder{
	
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
		
		int bsLen = buffer.readableBytes();
		if(bsLen > 86){
			byte[] bs = new byte[bsLen];
			buffer.readBytes(bs);
			//解析数据
			int readed = 86;
			ByteBuffer bbf = ByteBuffer.allocate(readed);
			bbf = ByteBuffer.wrap(bs);
			
			System.out.println(JSON.toJSONString(bs));
			
			// 第一位：
			String type = bs[0] + "";
			// 第二位：
			String ver = bs[1] + "";
			// 第三位：
			String proto = bs[2] + "";
			
		}
		out.add("success");;

	}
}
